<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>数据测试 - 外卖平台</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            background: #f5f5f5;
            padding: 20px;
        }
        
        .container {
            max-width: 1200px;
            margin: 0 auto;
            background: white;
            border-radius: 12px;
            padding: 30px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        
        h1 {
            color: #ff6b6b;
            margin-bottom: 30px;
            text-align: center;
        }
        
        .section {
            margin-bottom: 30px;
            padding: 20px;
            border: 1px solid #eee;
            border-radius: 8px;
        }
        
        .section h2 {
            color: #333;
            margin-bottom: 15px;
        }
        
        .btn {
            padding: 10px 20px;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            font-weight: 500;
            margin: 5px;
            transition: all 0.3s ease;
        }
        
        .btn-primary {
            background: #007bff;
            color: white;
        }
        
        .btn-danger {
            background: #dc3545;
            color: white;
        }
        
        .btn-success {
            background: #28a745;
            color: white;
        }
        
        .btn:hover {
            transform: translateY(-2px);
        }
        
        .stats {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 15px;
            margin-bottom: 20px;
        }
        
        .stat-card {
            background: #f8f9fa;
            padding: 15px;
            border-radius: 8px;
            text-align: center;
        }
        
        .stat-number {
            font-size: 24px;
            font-weight: bold;
            color: #ff6b6b;
        }
        
        .stat-label {
            color: #666;
            font-size: 14px;
        }
        
        .data-list {
            max-height: 300px;
            overflow-y: auto;
            border: 1px solid #eee;
            border-radius: 8px;
            padding: 10px;
        }
        
        .data-item {
            padding: 10px;
            border-bottom: 1px solid #eee;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        
        .data-item:last-child {
            border-bottom: none;
        }
        
        .message {
            padding: 12px;
            border-radius: 8px;
            margin-bottom: 20px;
            font-weight: 500;
            text-align: center;
        }
        
        .message.success {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        
        .message.error {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>🧪 数据测试页面</h1>
        
        <div id="message"></div>
        
        <!-- 操作按钮 -->
        <div class="section">
            <h2>🔧 操作</h2>
            <button class="btn btn-primary" onclick="loadAllData()">🔄 加载所有数据</button>
            <button class="btn btn-danger" onclick="clearAllData()">🗑️ 清空所有数据</button>
            <button class="btn btn-success" onclick="reinitData()">🔄 重新初始化数据</button>
            <a href="index.html" class="btn btn-primary">🏠 返回首页</a>
        </div>
        
        <!-- 数据统计 -->
        <div class="section">
            <h2>📊 数据统计</h2>
            <div class="stats" id="stats">
                <!-- 统计数据将在这里显示 -->
            </div>
        </div>
        
        <!-- 用户数据 -->
        <div class="section">
            <h2>👥 用户数据</h2>
            <div class="data-list" id="usersList">
                <!-- 用户列表将在这里显示 -->
            </div>
        </div>
        
        <!-- 商家数据 -->
        <div class="section">
            <h2>🏪 商家数据</h2>
            <div class="data-list" id="merchantsList">
                <!-- 商家列表将在这里显示 -->
            </div>
        </div>
        
        <!-- 菜品数据 -->
        <div class="section">
            <h2>🍽️ 菜品数据</h2>
            <div class="data-list" id="dishesList">
                <!-- 菜品列表将在这里显示 -->
            </div>
        </div>
    </div>

    <script src="database.js"></script>
    <script>
        // 初始化数据库
        const db = new Database();
        
        // 显示消息
        function showMessage(text, type) {
            const messageDiv = document.getElementById('message');
            messageDiv.innerHTML = `<div class="message ${type}">${text}</div>`;
            setTimeout(() => {
                messageDiv.innerHTML = '';
            }, 5000);
        }
        
        // 加载所有数据
        function loadAllData() {
            try {
                const users = db.getUsers();
                const merchants = db.getMerchants();
                const dishes = db.getDishes();
                const orders = db.getOrders();
                
                // 更新统计
                document.getElementById('stats').innerHTML = `
                    <div class="stat-card">
                        <div class="stat-number">${users.length}</div>
                        <div class="stat-label">用户数量</div>
                    </div>
                    <div class="stat-card">
                        <div class="stat-number">${merchants.length}</div>
                        <div class="stat-label">商家数量</div>
                    </div>
                    <div class="stat-card">
                        <div class="stat-number">${dishes.length}</div>
                        <div class="stat-label">菜品数量</div>
                    </div>
                    <div class="stat-card">
                        <div class="stat-number">${orders.length}</div>
                        <div class="stat-label">订单数量</div>
                    </div>
                `;
                
                // 显示用户数据
                document.getElementById('usersList').innerHTML = users.map(user => `
                    <div class="data-item">
                        <div>
                            <strong>${user.name || user.username}</strong>
                            <span style="color: #666; margin-left: 10px;">(${user.type})</span>
                        </div>
                        <div style="color: #666; font-size: 12px;">${user.phone || 'N/A'}</div>
                    </div>
                `).join('');
                
                // 显示商家数据
                document.getElementById('merchantsList').innerHTML = merchants.map(merchant => `
                    <div class="data-item">
                        <div>
                            <strong>${merchant.name}</strong>
                            <span style="color: #666; margin-left: 10px;">(${merchant.type})</span>
                        </div>
                        <div style="color: #666; font-size: 12px;">评分: ${merchant.rating} | 配送费: ¥${merchant.deliveryFee}</div>
                    </div>
                `).join('');
                
                // 显示菜品数据（只显示前20个）
                const dishesToShow = dishes.slice(0, 20);
                document.getElementById('dishesList').innerHTML = dishesToShow.map(dish => `
                    <div class="data-item">
                        <div>
                            <strong>${dish.name}</strong>
                            <span style="color: #666; margin-left: 10px;">¥${dish.price}</span>
                        </div>
                        <div style="color: #666; font-size: 12px;">${dish.category} | 库存: ${dish.stock}</div>
                    </div>
                `).join('') + (dishes.length > 20 ? `<div style="text-align: center; color: #666; padding: 10px;">... 还有 ${dishes.length - 20} 个菜品</div>` : '');
                
                showMessage('数据加载成功！', 'success');
            } catch (error) {
                showMessage('数据加载失败：' + error.message, 'error');
            }
        }
        
        // 清空所有数据
        function clearAllData() {
            if (confirm('确定要清空所有数据吗？此操作不可恢复！')) {
                try {
                    db.clearAllData();
                    showMessage('所有数据已清空！', 'success');
                    loadAllData();
                } catch (error) {
                    showMessage('清空数据失败：' + error.message, 'error');
                }
            }
        }
        
        // 重新初始化数据
        function reinitData() {
            if (confirm('确定要重新初始化数据吗？这将清空所有现有数据！')) {
                try {
                    db.forceReinit();
                    showMessage('数据重新初始化成功！', 'success');
                    loadAllData();
                } catch (error) {
                    showMessage('重新初始化失败：' + error.message, 'error');
                }
            }
        }
        
        // 页面加载时自动加载数据
        window.onload = function() {
            loadAllData();
        };
    </script>
</body>
</html>
